package com.xudow.app.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.xudow.app.R;
import com.xudow.app.model.OrderClassCard;
import com.xudow.app.ui.task.SiteCardTask;
import com.xudow.app.ui.task.UserProfileTask;
import com.xudow.app.util.DateUtil;
import com.xudow.app.util.Maps;
import com.xudow.app.util.PriceUtil;

import java.util.HashMap;

/**
 * 听课证
 */
public class SiteCardActivity extends BaseActivity {

    private Long orderItemId;

    private OrderClassCard occ;

    private SiteCardTask siteCardTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_site_card);

        orderItemId = getIntent().getLongExtra("orderItemId", 0L);

        showLodingDialog(getString(R.string.loading));

        HashMap<String, String> params = Maps.newHashMap();
        params.put("orderItemId", Long.toString(orderItemId));

        siteCardTask = new SiteCardTask(this, siteCardTaskHandler);
        addTask(siteCardTask);
        siteCardTask.execute(params);

    }

    Handler siteCardTaskHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            hideLoadingDialog();

            if (SiteCardTask.SUCCESS == msg.what) {
                occ = (OrderClassCard) msg.getData().getSerializable("occ");

                showData();
            } else {
                Toast.makeText(SiteCardActivity.this,
                        getString(R.string.loading_fail),
                        Toast.LENGTH_SHORT).show();
            }
        }
    };

    private void showData() {
        if (null == occ) {
            return;
        }

        setViewText(R.id.username, occ.getStudentName()); //姓名
        if (occ.getStudentGender().equals("2"))
            setViewText(R.id.gender, "女"); //性别
        else
            setViewText(R.id.gender,"男");
        setViewText(R.id.platform_account, ""+getMyApp().getUserProfileId()); //TODO：平台账号
        setViewText(R.id.platform_password, ""); //TODO：平台密码
        setViewText(R.id.clazz_name, occ.getClassName()); //班级名称
        setViewText(R.id.season, occ.getSemesterName()); //学季
        setViewText(R.id.course_time, DateUtil.dateToStr(occ.getOpenClassTime(),
                DateUtil.DATE_FORMAT_YYYY_MM_DD)); //开课时间
        setViewText(R.id.course_start_time, occ.getGiveTime()); //上课时间
        setViewText(R.id.clazz_count, "" + occ.getSempriceNumber()); //次数
        if (null != occ.getSiterow() && null != occ.getSitecolumn()) {
            setViewText(R.id.desk_num, String.format("%d-%d ", occ.getSitecolumn()+1, occ.getSiterow()+1)); //座位号
        }else
            setViewText(R.id.desk_num,"未选座");
        setViewText(R.id.discount, PriceUtil.format(occ.getItemsDiscount())); //折扣
        setViewText(R.id.deduction, PriceUtil.format(occ.getItemsReducePrice())); //减免
        setViewText(R.id.special_discount, PriceUtil.format(occ.getItemsSpecialPrice())); //特惠
        setViewText(R.id.coupon, PriceUtil.format(occ.getItemsCouponsPrice())); //优惠券
        setViewText(R.id.price, PriceUtil.format(occ.getCoursePrice())); //原价
        setViewText(R.id.actual_price, PriceUtil.format(occ.getItemsPrice())); //实付
        setViewText(R.id.consultant, ""); //TODO：咨询员
        setViewText(R.id.casher, ""); //TODO：收费员
        setViewText(R.id.school, occ.getSchoolName()); //报读校区
    }

    /**
     * 关闭按钮点击
     *
     * @param view
     */
    public void onXClick(View view) {
        finish();
    }

    private void setViewText(int resId, String text) {
        ((TextView) findViewById(resId)).setText(text);
    }
}
